// Copyright (c) 2017 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// simple-client is an example VPP management application that exercises the
// govpp API on real-world use-cases.
package main

import (
	"fmt"
	"strings"

	"git.fd.io/govpp.git/api"
	interfaces "github.com/pan2za/qos-client/binapi/interface"
)

var g_interfaces map[string]uint32 = make(map[string]uint32)

func interface_dump_at_startup(ch api.Channel) {
	if len(g_interfaces) > 0 {
		return
	}
	fmt.Println("Dumping interfaces..")

	// g_interfaces := make(map[string]uint32)
	n := 0
	reqCtx := ch.SendMultiRequest(&interfaces.SwInterfaceDump{})
	for {
		ifDetails := &interfaces.SwInterfaceDetails{}
		stop, err := reqCtx.ReceiveReply(ifDetails)
		if stop {
			break
		}
		if err != nil {
			logError(err, "dumping interfaces")
			return
		}
		n++
		fmt.Printf(" - interface #%d: %+v\n", n, ifDetails)
		internalName := strings.TrimRight(ifDetails.InterfaceName, "\x00")
		g_interfaces[internalName] = uint32(ifDetails.SwIfIndex)
	}

	fmt.Println("OK")
	fmt.Println()
}
func get_ifidx(ifname string) (uint32, bool) {
	idx, ok := g_interfaces[ifname]
	return idx, ok
}
